package com.xauto.pioneer.appuser.activity.mine.balance;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.activity.BaseActivity;
import com.xauto.pioneer.appuser.activity.common.PayUtil;
import com.xauto.pioneer.appuser.adapter.PaymentAdapter;
import com.xauto.pioneer.appuser.http.BaseLHttpHandler;
import com.xauto.pioneer.appuser.http.Constants;
import com.xauto.pioneer.appuser.http.LHttpLib;
import com.xauto.pioneer.appuser.interfaces.MemberCallBack;
import com.xauto.pioneer.appuser.model.JSONStatus;
import com.xauto.pioneer.appuser.model.Member;
import com.xauto.pioneer.appuser.model.MemberDeposit;
import com.xauto.pioneer.appuser.model.Payment;
import com.xauto.pioneer.appuser.util.DataCheckUtil;
import com.xauto.pioneer.appuser.util.DataLoadUtil;
import com.xauto.pioneer.appuser.util.StringUtils;
import com.xauto.pioneer.appuser.util.ViewUtil;
import com.xauto.pioneer.appuser.view.PromptDialog;
import com.xauto.pioneer.appuser.view.TopBar;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

/***
 * 押金充值 页面
 * @author Lemon Tue 7 Mar 17:24
 */
public class DepositTopUpActivity extends BaseActivity {

    @Bind(R.id.topBar) TopBar topBar;
    /** 显示-押金金额*/
    @Bind(R.id.tvDepositTopUpCost) TextView tvDepositTopUpCost;
    /** 显示-押金说明*/
    @Bind(R.id.tvDepositPaymentTip) TextView tvDepositPaymentTip;
    /** 保证金提现金额输入*/
    @Bind(R.id.tvDepositNum)
    EditText tvDepositNum;
    /** 支付按钮*/
    @Bind(R.id.tvDepositTopUpConfirmPay)
    TextView tvDepositTopUpConfirmPay;
//    /** 点击选中-微信支付*/
//    @Bind(R.id.ivDepositTopUpChooseWeChat) ImageView ivDepositTopUpChooseWeChat;
//    /** 点击选中-支付宝支付*/
//    @Bind(R.id.ivDepositTopUpChooseAliPay) ImageView ivDepositTopUpChooseAliPay;
    /** 押金支付方式列表*/
    @Bind(R.id.listPayment)
    ListView listPayment;
    /** 支付方式适配器*/
    private PaymentAdapter paymentAdapter;
    private PromptDialog promptDialog;
    private String paymentCode = Constants.PAY_TYPE_WECHAT;
    /**判断状态*/
    @Bind(R.id.user_stats)
    RelativeLayout user_stats;
    @Bind(R.id.layoutDepositPayment_Status)
    View layoutDepositPayment_Status;
    private Member member;
    @Override
    protected int getContentView() {
        return R.layout.ac_deposit_top_up;
    }

    @Override
    protected void initView() {
        member = DataCheckUtil.getMemberObj(mContext);
        if(member.member_deposit_info.is_deposit.equals("1")){
            layoutDepositPayment_Status.setVisibility(View.VISIBLE);
            user_stats.setVisibility(View.GONE);
        }else{
            layoutDepositPayment_Status.setVisibility(View.GONE);
            user_stats.setVisibility(View.VISIBLE);
        }
        //注册EventBus
        EventBus.getDefault().register(this);
        promptDialog = new PromptDialog(mContext, getString(R.string.notice), getString(R.string.text_not_network),
                getString(R.string.to_setting), getString(R.string.logout),getString(R.string.dialogpic_notice), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //去设置
                promptDialog.dismiss();
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //退出
                promptDialog.dismiss();
            }
        });

        listPayment.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(!paymentAdapter.getDataList().get(position).isChoose) {
                    for (int i = 0; i < paymentAdapter.getDataList().size(); i++) {
                        paymentAdapter.getDataList().get(i).isChoose = false;
                    }
                    paymentAdapter.getDataList().get(position).isChoose = true;
                    paymentAdapter.notifyDataSetChanged();
                    paymentCode = paymentAdapter.getDataList().get(position).getPayment_code();
                }
            }
        });
        tvDepositNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String Num = tvDepositNum.getText().toString().trim();
                if (!StringUtils.isBlank(Num) && !StringUtils.isBlank(s.toString())){
                    ViewUtil.GetViewShgape(tvDepositTopUpConfirmPay);
                    tvDepositTopUpConfirmPay.setClickable(true);
                    tvDepositTopUpConfirmPay.setBackgroundResource(R.drawable.bg_btn_green_selector);
                    ViewUtil.SetViewShgape(tvDepositTopUpConfirmPay);
                }else {
                    ViewUtil.GetViewShgape(tvDepositTopUpConfirmPay);
                    tvDepositTopUpConfirmPay.setClickable(false);
                    tvDepositTopUpConfirmPay.setBackgroundResource(R.drawable.bg_btn_gray);
                    ViewUtil.SetViewShgape(tvDepositTopUpConfirmPay);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected void initData() {
        topBar.showView(TopBar.LEFT_IMAGE_BTN, TopBar.TITLE);
        topBar.setImage(TopBar.LEFT_IMAGE_BTN, R.mipmap.ic_common_left);
        topBar.setText(TopBar.TITLE, R.string.deposit_top_up);
        topBar.setLeftOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        paymentAdapter = new PaymentAdapter(this, R.layout.item_payment);
        listPayment.setAdapter(paymentAdapter);

        List<Payment> payments = new Payment().getPaymentListForOnLine(mContext);
        paymentAdapter.getDataList().addAll(payments);
        paymentAdapter.notifyDataSetChanged();
        getDepositDetail();

    }

    @OnClick({R.id.tvDepositTopUpConfirmPay})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvDepositTopUpConfirmPay:
                //确认支付
                for (int i = 0; i < paymentAdapter.getDataList().size(); i++) {
                    if(paymentAdapter.getDataList().get(i).isChoose) {
                        paymentCode = paymentAdapter.getDataList().get(i).getPayment_code();
                    }
                }
                loadGetPaymentOrderParam();
                break;
        }
    }

    /** 获取支付保证金订单的支付数据*/
    void loadGetPaymentOrderParam() {
        if(tvDepositNum.getText().toString().equals("")){
           return;
        }
        new LHttpLib().submitDeposit(mContext, memberId,tvDepositNum.getText().toString(),paymentCode, new BaseLHttpHandler(mContext, true, true) {

            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
                String payment_order_param = jsonStatus.data.optString("payment_order_param");
                new PayUtil(DepositTopUpActivity.this, payment_order_param,paymentCode);
            }
        });
    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
    /**
     * 接受支付成功的通知信息
     * @param paymentCode
     */
    public void onEventMainThread(String paymentCode){
        switch (paymentCode){
            //支付宝支付成功
            case Constants.PAY_TYPE_ALIPAY:
                rechargeSuccess();
                break;
            //微信支付成功
            case Constants.PAY_TYPE_WECHAT:
                rechargeSuccess();
                break;
        }
    }

    /**
     *获取保证金详情
     */
    private void getDepositDetail(){
        //获取保证金详情
        MemberDeposit memberDeposit = DataCheckUtil.getMemberObj(mContext).member_deposit_info;
        tvDepositTopUpCost.setText("押金应缴纳："+memberDeposit.required_deposit_amount_format);
        tvDepositPaymentTip.setText(memberDeposit.required_deposit_amount_remark);
        if ("1".equals(memberDeposit.required_deposit_amount_remark_style)){
            tvDepositPaymentTip.setTextColor(mContext.getResources().getColor(R.color.main_red));
        }else {
            tvDepositPaymentTip.setTextColor(mContext.getResources().getColor(R.color.black));
        }
    }
    /**
     * 充值成功
     */
    private void  rechargeSuccess(){
        // TODO: 2017/6/8
        prompt("余额充值成功");
        //刷新数据
        DataLoadUtil.refreshMemberData(mContext, new MemberCallBack() {
            @Override
            public void getMember(Member member) {
                finish();
            }
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //反注册EventBus
        EventBus.getDefault().unregister(this);
    }
    @Override
    protected void onResume() {
        super.onResume();
        //刷新页面
        DataLoadUtil.refreshMemberData(mContext, new MemberCallBack() {
            @Override
            public void getMember(Member m) {
                member = m;
                if(member.member_deposit_info.is_deposit.equals("1")){
                    layoutDepositPayment_Status.setVisibility(View.VISIBLE);
                    user_stats.setVisibility(View.GONE);
                }else{
                    layoutDepositPayment_Status.setVisibility(View.GONE);
                    user_stats.setVisibility(View.VISIBLE);
                }
            }
        });
    }
}
